import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class threeSum {
    public static void main(String[] args) {
        int[] nums = new int[]{-1,0,1,2,-1,-4};
        System.out.println(threeSum(nums));
    }
    public static List<List<Integer>> threeSum(int[] nums) {
        int len = nums.length;
        List<List<Integer>> res = new ArrayList<>();

        Arrays.sort(nums);

        if(len<3) return res;
        for(int k=0;k<len;k++){
            if(k>0 && nums[k]==nums[k-1]) continue;
            int num = -nums[k];
            int j = len-1;
            for(int i=k+1;i<len;i++){
                if(i>k+1 && nums[i]==nums[i-1]) continue;
               while(j>i && nums[j]+nums[i]>num) j--;
               if(j==i) break;
               if(num == nums[i]+nums[j]){
                   List<Integer> re = new ArrayList<>();
                   re.add(nums[k]);
                   re.add(nums[i]);
                   re.add(nums[j]);
                   res.add(re);
               }
            }

        }
        return res;

    }
}
